<html>
<head><meta charset="utf-8"><title>fn kind() -&gt; TypeKind or -&gt; &amp;TypeKind · wg-traits · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/index.html">wg-traits</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/fn.20kind().20-.3E.20TypeKind.20or.20-.3E.20.26TypeKind.html">fn kind() -&gt; TypeKind or -&gt; &amp;TypeKind</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="205762268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/fn%20kind%28%29%20-%3E%20TypeKind%20or%20-%3E%20%26TypeKind/near/205762268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/fn.20kind().20-.3E.20TypeKind.20or.20-.3E.20.26TypeKind.html#205762268">(Aug 03 2020 at 06:03)</a>:</h4>
<p>Can we change<code>kind</code> to return <code>TypeKind</code> instead of <code>&amp;TypeKind</code>?</p>
<p>This would improve pattern matching quite a lot imo</p>



<a name="205765169"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/fn%20kind%28%29%20-%3E%20TypeKind%20or%20-%3E%20%26TypeKind/near/205765169" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/fn.20kind().20-.3E.20TypeKind.20or.20-.3E.20.26TypeKind.html#205765169">(Aug 03 2020 at 07:13)</a>:</h4>
<p>We can’t without moving values, right? TyKind is not Copy AFAICT</p>



<a name="205768049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/fn%20kind%28%29%20-%3E%20TypeKind%20or%20-%3E%20%26TypeKind/near/205768049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/fn.20kind().20-.3E.20TypeKind.20or.20-.3E.20.26TypeKind.html#205768049">(Aug 03 2020 at 07:58)</a>:</h4>
<p>It really should be <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span> Let me check why it isn't <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="205768068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/fn%20kind%28%29%20-%3E%20TypeKind%20or%20-%3E%20%26TypeKind/near/205768068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/fn.20kind().20-.3E.20TypeKind.20or.20-.3E.20.26TypeKind.html#205768068">(Aug 03 2020 at 07:59)</a>:</h4>
<p>Everything works if we derive <code>Copy</code> there</p>



<a name="205768086"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/fn%20kind%28%29%20-%3E%20TypeKind%20or%20-%3E%20%26TypeKind/near/205768086" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/fn.20kind().20-.3E.20TypeKind.20or.20-.3E.20.26TypeKind.html#205768086">(Aug 03 2020 at 07:59)</a>:</h4>
<p>tbh I really don't see why it's not rn, maybe an oversight <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="205768138"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/fn%20kind%28%29%20-%3E%20TypeKind%20or%20-%3E%20%26TypeKind/near/205768138" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/fn.20kind().20-.3E.20TypeKind.20or.20-.3E.20.26TypeKind.html#205768138">(Aug 03 2020 at 08:00)</a>:</h4>
<p>or we previously had something which was not copy there and now don't</p>



<a name="205768248"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/fn%20kind%28%29%20-%3E%20TypeKind%20or%20-%3E%20%26TypeKind/near/205768248" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/fn.20kind().20-.3E.20TypeKind.20or.20-.3E.20.26TypeKind.html#205768248">(Aug 03 2020 at 08:01)</a>:</h4>
<p>so IMO we should derive Copy and change <code>kind</code>, but maybe there's a reason why <code>TyKind</code> is not <code>Copy</code> rn</p>



<a name="205768758"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/fn%20kind%28%29%20-%3E%20TypeKind%20or%20-%3E%20%26TypeKind/near/205768758" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/fn.20kind().20-.3E.20TypeKind.20or.20-.3E.20.26TypeKind.html#205768758">(Aug 03 2020 at 08:07)</a>:</h4>
<p>Also, will it be <code>Copy</code> in the new type library?</p>



<a name="205768846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/fn%20kind%28%29%20-%3E%20TypeKind%20or%20-%3E%20%26TypeKind/near/205768846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/fn.20kind().20-.3E.20TypeKind.20or.20-.3E.20.26TypeKind.html#205768846">(Aug 03 2020 at 08:08)</a>:</h4>
<p>I hope so</p>



<a name="205769756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/fn%20kind%28%29%20-%3E%20TypeKind%20or%20-%3E%20%26TypeKind/near/205769756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/fn.20kind().20-.3E.20TypeKind.20or.20-.3E.20.26TypeKind.html#205769756">(Aug 03 2020 at 08:19)</a>:</h4>
<p>Since TypeKind can be copy in both RA and rustc currently, I suppose we can just make that a requirement in the type library</p>



<a name="208260749"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/fn%20kind%28%29%20-%3E%20TypeKind%20or%20-%3E%20%26TypeKind/near/208260749" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/fn.20kind().20-.3E.20TypeKind.20or.20-.3E.20.26TypeKind.html#208260749">(Aug 27 2020 at 18:57)</a>:</h4>
<p>I'd be ok with that change</p>



<a name="208267835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/fn%20kind%28%29%20-%3E%20TypeKind%20or%20-%3E%20%26TypeKind/near/208267835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/fn.20kind().20-.3E.20TypeKind.20or.20-.3E.20.26TypeKind.html#208267835">(Aug 27 2020 at 19:57)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/144729-wg-traits/topic/Looking.20to.20contribute/near/205762268">said</a>:</p>
<blockquote>
<p>Can we change<code>kind</code> to return <code>TypeKind</code> instead of <code>&amp;TypeKind</code>?</p>
<p>This would improve pattern matching quite a lot imo</p>
</blockquote>
<p><span class="user-mention" data-user-id="255061">@LeSeulArtichaut</span> did you investigate making this change? Given that the MCP is approved, and we saw that perf looked good, I would r+ the PR -- but I do think that change may be worth making (actually, we ought to measure the perf afterwards too -- maybe best to make it without a rebase first to keep the comparison valid?)</p>



<a name="208373376"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/fn%20kind%28%29%20-%3E%20TypeKind%20or%20-%3E%20%26TypeKind/near/208373376" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/fn.20kind().20-.3E.20TypeKind.20or.20-.3E.20.26TypeKind.html#208373376">(Aug 28 2020 at 17:38)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/144729-wg-traits/topic/Looking.20to.20contribute/near/208267835">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/144729-wg-traits/topic/Looking.20to.20contribute/near/205762268">said</a>:</p>
<blockquote>
<p>Can we change<code>kind</code> to return <code>TypeKind</code> instead of <code>&amp;TypeKind</code>?</p>
<p>This would improve pattern matching quite a lot imo</p>
</blockquote>
<p><span class="user-mention silent" data-user-id="255061">LeSeulArtichaut</span> did you investigate making this change? Given that the MCP is approved, and we saw that perf looked good, I would r+ the PR -- but I do think that change may be worth making (actually, we ought to measure the perf afterwards too -- maybe best to make it without a rebase first to keep the comparison valid?)</p>
</blockquote>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> I thnk you won't be able to do a try build if there are merge conflicts, right?</p>



<a name="208380521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/fn%20kind%28%29%20-%3E%20TypeKind%20or%20-%3E%20%26TypeKind/near/208380521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/fn.20kind().20-.3E.20TypeKind.20or.20-.3E.20.26TypeKind.html#208380521">(Aug 28 2020 at 18:42)</a>:</h4>
<p><span class="user-mention" data-user-id="255061">@LeSeulArtichaut</span> I'm not sure actaully</p>



<a name="208380530"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/fn%20kind%28%29%20-%3E%20TypeKind%20or%20-%3E%20%26TypeKind/near/208380530" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/fn.20kind().20-.3E.20TypeKind.20or.20-.3E.20.26TypeKind.html#208380530">(Aug 28 2020 at 18:42)</a>:</h4>
<p>I guess prob a good idea to rebase regardless</p>



<a name="208381461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/fn%20kind%28%29%20-%3E%20TypeKind%20or%20-%3E%20%26TypeKind/near/208381461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/fn.20kind().20-.3E.20TypeKind.20or.20-.3E.20.26TypeKind.html#208381461">(Aug 28 2020 at 18:51)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116113">lqd</span> had tried <a href="https://github.com/rust-lang/rust/pull/75077#issuecomment-674206704">earlier</a> and it didn't work, but at the same time, in the homu queue, the "mergable state" is blank...</p>



<a name="208401222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/fn%20kind%28%29%20-%3E%20TypeKind%20or%20-%3E%20%26TypeKind/near/208401222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/fn.20kind().20-.3E.20TypeKind.20or.20-.3E.20.26TypeKind.html#208401222">(Aug 28 2020 at 22:25)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> I resolved the conflicts, if you want to "keep the comparison fair" you might want to re-run perf now? I'll try work on changing <code>kind() -&gt; &amp;TyKind</code> to <code>kind() -&gt; TyKind</code> tomorrow.</p>



<a name="208444172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/fn%20kind%28%29%20-%3E%20TypeKind%20or%20-%3E%20%26TypeKind/near/208444172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/fn.20kind().20-.3E.20TypeKind.20or.20-.3E.20.26TypeKind.html#208444172">(Aug 29 2020 at 17:15)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> I found a disadvantage to having <code>ty.kind()</code> returning <code>TyKind</code>. Consider the following code (from <code>src/librustc_middle/ty/codec.rs</code>):</p>
<div class="codehilite"><pre><span></span><code><span class="k">impl</span><span class="o">&lt;</span><span class="na">&#39;tcx</span><span class="p">,</span><span class="w"> </span><span class="n">E</span>: <span class="nc">TyEncoder</span><span class="o">&lt;</span><span class="na">&#39;tcx</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="n">EncodableWithShorthand</span><span class="o">&lt;</span><span class="na">&#39;tcx</span><span class="p">,</span><span class="w"> </span><span class="n">E</span><span class="o">&gt;</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Ty</span><span class="o">&lt;</span><span class="na">&#39;tcx</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">Variant</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ty</span>::<span class="n">TyKind</span><span class="o">&lt;</span><span class="na">&#39;tcx</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="cp">#[inline]</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">variant</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">Self</span>::<span class="n">Variant</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;</span><span class="bp">self</span><span class="p">.</span><span class="n">kind</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>This doesn't compile, because <code>kind()</code> creates a copy of the <code>TyKind</code>:</p>
<div class="codehilite"><pre><span></span><code>error[E0515]: cannot return reference to temporary value
  --&gt; src/librustc_middle/ty/codec.rs:42:9
   |
42 |         &amp;self.kind()
   |         ^-----------
   |         ||
   |         |temporary value created here
   |         returns a reference to data owned by the current function
</code></pre></div>


<p>And I don't think there is any workaround possible, other than accessing <code>ty.kind</code> directly without going through the method.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>